<?php

class ArrayModel extends PathModel
{

    function __construct($path, $c)
    {
        parent::__construct($path, $c);
    }

    function push($id, $item)
    {
        $result = $this->c->update(['id' => $id],
            [
                '$push' => [join('.', $this->path) => $item],
                '$set' => ['stat.updateTime' => time()]
            ]
        );

        if (result['n'] === 0) throw IdNotExistException($id, $this->c->getName());
	}

    function pop($id, $item)
    {
        $result = $this->c->update(['id' => $id],
            [
                '$pull' => [join('.', $this->path) => $item],
                '$set' => ['stat.updateTime' => time()]
            ]
        );

        if (result['n'] === 0) throw IdNotExistException($id, $this->c->getName());
	}

    function verify($value)
    {
        if (!is_array($value) ||
            (!empty($value) && !key_exists(0, $value))
        ) {
            throw new IllegalModelException(
                'Expect Array', $this->path, $this->c->getName());
        }
    }

}
